#ifndef PROGRESS_BAR
#define PROGRESS_BAR
#include <math.h>

#include <atlbase.h>
#include <atlapp.h>
#include <atlgdi.h>
#include <atlmisc.h>
#include <atlwin.h>
#include <atlcrack.h>

#include "Player.h"
#include "PlayerState.h"

class MainWindow;

class ProgressBar:public CWindowImpl<ProgressBar, CWindow, CControlWinTraits>
{
public:
    DECLARE_WND_CLASS(_T("REPEATER_WIN"))
    
    BEGIN_MSG_MAP_EX(ProgressBar)
        MSG_WM_PAINT(OnPaint)
        MSG_WM_CREATE(OnCreate)
        MSG_WM_DESTROY(OnDestroy)
        MSG_WM_SETCURSOR(OnSetCursor)
        MSG_WM_LBUTTONDOWN(OnButtonDown )
    END_MSG_MAP()


    ProgressBar(MainWindow& main_window);
    
    LRESULT OnSetCursor(HWND , UINT , UINT );
    int OnCreate(LPCREATESTRUCT lpCreateStruct);
    void OnPaint(CDCHandle dc);

    LRESULT OnDestroy();
    LRESULT OnButtonDown( UINT WPARAM, CPoint point);

    //Normal playing 
    void SetLength(long length);
    void StartPlay(long pos);
    void UpdateNormalPlayingPosition(long pre_pos, long new_pos);
    //Repeated playing
    void SetStartPos(long pos);
    void SetEndPos(long pos);
    void StartRepeat();    
    void UpdateRepeatedPlayingPosition(long pre_pos, long new_pos);

    static int CalculateX(long pos, long length, int width){
        return static_cast<int> (ceil( width * static_cast<double> (pos) / length));
    }

private:
    MainWindow& main_window_;
    Rect rect0_, rect1_, rect2_;
    Rect play_rect_;//playing progress info
    Rect repeat_rect_;//repeating progress info

    //UI style related
    LinearGradientBrush* play_brush_, *repeat_brush_;
    SolidBrush* background_brush_;

    //file info
    long length_;
    long start_pos_;
    long end_pos_;
    long current_pos_;

    bool is_normal_playing_;
    bool is_repeated_playing_;
    
private:
    void DrawRectangle(Graphics& gfx, Brush* brush, long from, long to);
};
#endif
